package cn.iocoder.yudao.module.shop.controller.admin.daystatistics;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.daystatistics.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.daystatistics.ShopDayStatisticsDO;
import cn.iocoder.yudao.module.shop.convert.daystatistics.ShopDayStatisticsConvert;
import cn.iocoder.yudao.module.shop.service.daystatistics.ShopDayStatisticsService;

@Tag(name = "管理后台 - 每日手工费统计")
@RestController
@RequestMapping("/shop/day-statistics")
@Validated
public class ShopDayStatisticsController {

    @Resource
    private ShopDayStatisticsService dayStatisticsService;

    @PostMapping("/create")
    @Operation(summary = "创建每日手工费统计")
    @PreAuthorize("@ss.hasPermission('shop:day-statistics:create')")
    public CommonResult<Long> createDayStatistics(@Valid @RequestBody ShopDayStatisticsCreateReqVO createReqVO) {
        return success(dayStatisticsService.createDayStatistics(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新每日手工费统计")
    @PreAuthorize("@ss.hasPermission('shop:day-statistics:update')")
    public CommonResult<Boolean> updateDayStatistics(@Valid @RequestBody ShopDayStatisticsUpdateReqVO updateReqVO) {
        dayStatisticsService.updateDayStatistics(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除每日手工费统计")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:day-statistics:delete')")
    public CommonResult<Boolean> deleteDayStatistics(@RequestParam("id") Long id) {
        dayStatisticsService.deleteDayStatistics(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得每日手工费统计")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:day-statistics:query')")
    public CommonResult<ShopDayStatisticsRespVO> getDayStatistics(@RequestParam("id") Long id) {
        ShopDayStatisticsDO dayStatistics = dayStatisticsService.getDayStatistics(id);
        return success(ShopDayStatisticsConvert.INSTANCE.convert(dayStatistics));
    }

    @GetMapping("/list")
    @Operation(summary = "获得每日手工费统计列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:day-statistics:query')")
    public CommonResult<List<ShopDayStatisticsRespVO>> getDayStatisticsList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopDayStatisticsDO> list = dayStatisticsService.getDayStatisticsList(ids);
        return success(ShopDayStatisticsConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得每日手工费统计分页")
    @PreAuthorize("@ss.hasPermission('shop:day-statistics:query')")
    public CommonResult<PageResult<ShopDayStatisticsRespVO>> getDayStatisticsPage(@Valid ShopDayStatisticsPageReqVO pageVO) {
        PageResult<ShopDayStatisticsDO> pageResult = dayStatisticsService.getDayStatisticsPage(pageVO);
        return success(ShopDayStatisticsConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出每日手工费统计 Excel")
    @PreAuthorize("@ss.hasPermission('shop:day-statistics:export')")
    @OperateLog(type = EXPORT)
    public void exportDayStatisticsExcel(@Valid ShopDayStatisticsExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopDayStatisticsDO> list = dayStatisticsService.getDayStatisticsList(exportReqVO);
        // 导出 Excel
        List<ShopDayStatisticsExcelVO> datas = ShopDayStatisticsConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "每日手工费统计.xls", "数据", ShopDayStatisticsExcelVO.class, datas);
    }

}
